package com.imyours.webservice.services;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.classic.Session;

import com.imyours.model.announcement.Announcement;
import com.imyours.model.user.UserEntity;
import com.imyours.model.util.HibernateUtil;
import com.imyours.webservice.service.Context;
import com.imyours.webservice.service.ContextService;
import com.imyours.webservice.services.annotation.Service;

/**
 * 获取公告信息
 * 
 * @author SLevenc
 * 
 */
@Service(name = "getAnnouncement")
public class GetAnnouncement extends ContextService {

	public GetAnnouncement(Context context) {
		super(context);
	}

	public void execute() {
		UserEntity user = getUserEntity();
		if (user != null) {
			Session se = HibernateUtil.getSessionFactory().openSession();
			String hql = "from Announcement ";

			List<Announcement> as = se.createQuery(hql).setMaxResults(30).list();
			List<Map<String, String>> resultList = new ArrayList<Map<String, String>>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			for (Announcement a : as) {
				Map<String, String> m = new HashMap<String, String>();
				m.put("sender", a.getSender().getNickname());
				m.put("date", sdf.format(a.getDate()));
				m.put("content", a.getContent());
				resultList.add(m);
			}
			se.close();
			context.putResult("announcement", resultList);
			context.success();
		}
	}
}
